package arithmetic;

/**
 * Created by AJie on 2019/4/26
 * Function : 二分查找 ，有序
 *
 *
 */
public class A_20_BinarySearch {
    public static void main(String[] args) {

        int [] origin = new int[10];
        for (int i = 0; i < 10; i++) {
            origin [i] = i*2+3;
        }
        int target = 9;
        int index = binarySearch(origin,target);
        System.out.println("index=="+index);

    }

    private static int binarySearch(int[] origin, int target) {
        int left = 0,right = origin.length-1;
        while (left<=right){
            int mid = left+(right-left)/2;//没有问题
//            int mid = (left+right)/2;//Integer.Max 溢出 据说有问题？？
            System.out.println("left=="+ left +"  right=="+right+" mid="+ mid+"  (right-left)/2"+(right-left)/2);
            if (origin[mid]==target){
                return mid;
            }else if (origin[mid]>target){
                right = mid - 1;
            }else {
                left = mid + 1;
            }
        }
        return -1;
    }
}
